﻿using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.Mysql;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Repository
{
    public class BaseRepository<TEntity> :IBaseRepository<TEntity> where TEntity : BaseEntity
    {
        [Autowired]
        public IMysqlDBBase _dbContext { get; set; }

        public int GetCount()
        {
            int count =_dbContext.Set<TEntity>().Count();
            return count;
        }
        public virtual async ValueTask<EntityEntry<TEntity>> Create(TEntity entity, int CreatorId)
        {

            return await _dbContext.Set<TEntity>().AddAsync(entity);
        }

        public virtual async Task<List<TEntity>> Create(List<TEntity> entitys, int CreatorId)
        {

            await _dbContext.Set<TEntity>().AddRangeAsync(entitys.ToArray());
            return entitys;

        }

        public virtual async Task<EntityEntry<TEntity>> Update(TEntity entity, int updateid)
        {
            return await Task.Run(() =>
            {
                return _dbContext.Set<TEntity>().Update(entity);
            });
        }
        public virtual async Task  Update(List<TEntity> entitys, int updateid)
        {
            await Task.Run(()=> { 
            _dbContext.Set<TEntity>().UpdateRange(entitys);
            });
        }
        public async Task Delete(Expression<Func<TEntity, bool>> lamada)
        {
            await Task.Run(() =>
            {
                var entities = _dbContext.Set<TEntity>().Where(lamada);
                _dbContext.Set<TEntity>().RemoveRange(entities);
            });
        }
        public virtual async Task SoftDelete(Expression<Func<TEntity, bool>> lamada, int deleterId)
        {
            var entities = await _dbContext.Set<TEntity>().Where(lamada).ToListAsync();
            _dbContext.Set<TEntity>().UpdateRange(entities);
        }
        public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> lamada)
        {
            return await _dbContext.Set<TEntity>().AnyAsync(lamada);
        }

        public  IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> lamada)
        {
            return  _dbContext.Set<TEntity>().AsNoTracking().Where(lamada);
        }
        public IQueryable<TEntity> GetQuery()
        {
            return _dbContext.Set<TEntity>().AsNoTracking();
        }
        /// <summary>
        /// 如果不存在返回为空
        /// </summary>
        /// <param name="lamada"></param>
        /// <returns></returns>

        public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> lamada)
        {
            return await _dbContext.Set<TEntity>().AsNoTracking().FirstOrDefaultAsync(lamada);
        }
        public async Task<bool> SaveChangesAsync()
        {
            return await _dbContext.SaveChangesAsync() > 0;
        }
        public bool SaveChanges()
        {
            
            return this._dbContext.SaveChanges() > 0;
        }
    }
}
